package by.ins.app.console;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import org.apache.log4j.Logger;

import by.ins.app.commands.AppCommnandProvider;
import by.yativem.commons.console.ConsoleCommandBean;

public class RemoteAdminConsoleImpl implements IRemoteAdminConsole {
	private static final Logger log = Logger.getLogger(RemoteAdminConsoleImpl.class);
	private AppCommnandProvider appCommnandProvider;
	
	private int port;
	private String name;

	Registry registry;

	public void init() {

		try {
			registry = LocateRegistry.createRegistry(port);
			IRemoteAdminConsole iRemoteAdminConsole = (IRemoteAdminConsole) UnicastRemoteObject.exportObject(this, port);
			registry.bind(name, iRemoteAdminConsole);
		} catch (Exception e) {			
			log.error("", e);
		}
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	

	
	@Override
	public Object sendCommand(ConsoleCommandBean command) throws RemoteException {
		Object result = appCommnandProvider.sendCommand(command);
		return result;
		
	}

	@Override
	public String test() throws RemoteException {
		
		return "RMI object";
		
	}

	public AppCommnandProvider getAppCommnandProvider() {
		return appCommnandProvider;
	}

	public void setAppCommnandProvider(AppCommnandProvider appCommnandProvider) {
		this.appCommnandProvider = appCommnandProvider;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
	
}
